﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;

namespace PersonalTrener
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            trayNotifyIcon.Text = Text;
            soundPathTextBox.Text = "Message.wav";
            picPathTextBox.Text = "Pic.jpg";
            SetTime();
            playTimer.Enabled = true;
        }

        private void trayNotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Visible = true;
                this.ShowInTaskbar = true;
                this.WindowState = FormWindowState.Normal;
            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.Visible = false;
            }
        }

        private List<DateTime> m_Time = new List<DateTime>();
        private void SetTime()
        {
            m_Time.Clear();
            try
            {
                foreach (string s in timeListBox.Items)
                {
                    m_Time.Add(DateTime.Parse(s));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Ошибка значений в списке: "+e.Message);
            }
        }

        private void playTimer_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < m_Time.Count; i++)
			{
                if (DateTime.Now > m_Time[i] && DateTime.Now < m_Time[i + 1])
                {
                    DoAlarm();
                    m_Time.RemoveAt(i);
                    i--;
                }
			}
        }

        private void DoAlarm()
        {
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = soundPathTextBox.Text;
            player.Play();
        }
    }
}
